package com.moore;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class ResetAutoIncrement {
    public static void main(String[] args) {
        String url = "jdbc:mysql://47.97.19.126:3306/recruitment-system?allowPublicKeyRetrieval=true&useSSL=false&allowMultiQueries=true";
        String user = "root";
        String password = "762094";

        try {
            // 建立数据库连接
            Connection conn = DriverManager.getConnection(url, user, password);
            Statement stmt = conn.createStatement();

            // 获取所有表名
            ResultSet rs = stmt.executeQuery("SHOW TABLES");
            while (rs.next()) {
                String tableName = rs.getString(1);
                // 为每个表创建一个新的Statement对象
                Statement truncateStmt = conn.createStatement();
                int i = truncateStmt.executeUpdate("ALTER TABLE `" + tableName + "` AUTO_INCREMENT = 1");
                System.out.println("Truncated table: " + tableName + ", affected rows: " + i);
                truncateStmt.close();
            }

            // 关闭ResultSet
            rs.close();
            // 关闭Statement
            stmt.close();
            // 关闭连接
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}